package UI;

import Data.BasicData;
import Data.BitmapRequest;
import Data.StringDealer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.cardview.widget.CardView;
import tbs.littlebaby.R;

import java.util.ArrayList;
import java.util.Calendar;

public class SerachADP extends BaseAdapter implements Filterable {
    BasicData[] datas;
    BasicData[] performs;
    Context context;
    public SerachADP(Context context, BasicData... datas)
    {
        this.context=context;
        this.datas=datas;
    }


    @Override
    public int getCount() {
        return performs.length;
    }

    @Override
    public Object getItem(int position) {
        return performs[position];
    }

    @Override
    public long getItemId(int position) {

        BasicData temp=performs[position];
        int type=temp.getType();
        int id=0;
        for (int i=0;i< datas.length;i++)
        {
            if(datas[i].getType()==type)
            {
                if(datas[i].equals(temp))
                    return id;
                else
                    id++;
            }
        }
        return -1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(context).inflate(R.layout.singlelistitem,null);
        CardView cardView= view.findViewById(R.id.CARD);
        TextView pd= view.findViewById(R.id.item_priceDate);
        BasicData data=performs[position];
        Calendar cal = Calendar.getInstance();
        String text;
        if(data.getDate()!=null)
        {
            cal.setTime(data.getDate());
            text =String.format("价格：%.2f元  \n购买日期：%d年%d月%d日",data.getPrice(),cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,cal.get(Calendar.DAY_OF_MONTH));
        }
        else
        {
            text =String.format("价格：%.2f元  \n购买日期：不详",data.getPrice());
        }

        pd.setText(text);
        pd=view.findViewById(R.id.item_tag);
        pd.setText(String.format("备注：%s",data.getTag()==null?"空备注":data.getTag()));
        TextView nb=view.findViewById(R.id.item_nameBrand);
        nb.setText(String.format("%s  品牌：%s",data.getName(),data.getBrand()));
        ImageView img=view.findViewById(R.id.item_image);
        if(data.getImage()!=null)
        {
            img.setImageBitmap(BitmapRequest.ScaleImage(data.getImage()));
        }
        return view;
    }
    private class ArrayFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(constraint!=null&&constraint.length()>0)
            {
                ArrayList<BasicData> tmp=new ArrayList<>();
                for(int i=0;i<datas.length;i++)
                {
                    if(StringDealer.DataToString(datas[i]).contains(constraint))
                        tmp.add(datas[i]);
                }
                results.values=tmp;
                results.count=tmp.size();
                return results;
            }
            return null;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if(results!=null)
            {
                ArrayList<BasicData> tmp=   (ArrayList<BasicData>)results.values;
                performs=new BasicData[results.count];
                tmp.toArray(performs);
                notifyDataSetChanged();
            }
            else
                notifyDataSetInvalidated();


        }
    }

    @Override
    public Filter getFilter() {
        return new ArrayFilter();
    }
}
